import time

from click import prompt
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class Base():

    # 初始化驱动
    def __init__(self,driver):
        #判断
        self.driver=driver

    # 打开浏览器
    def open_url(self,url):
        self.driver.get(url)

    # 封装定位方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
            print()

    # 点击事件
    def click(self,loc):
        self.find_element(loc).click()
    # 输入框输入事件
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)
    #封装text()文本断言
    def text(self,loc):
        return self.find_element(loc).text
    #封装clear操作
    def clear(self,loc):
        self.find_element(loc).clear()
    #滚动任意像素分装
    def scrollTo_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    #滚动到底部
    def scrollTo_H(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    #切换窗口
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #封装alert
    def alert_accept(self):
        alert = self.driver.switch_to.alert
    def confirm_dismiss(self):
        confirm=self.driver.switch_to.alert
        confirm.dismiss()
    def prompt_accept(self):
        prompt=self.driver.switch_to.alert
        prompt.send_keys("这是一prompt弹窗")
        prompt.accept()
    def prompt_dismiss(self):
        prompt = self.driver.switch_to.alert
        prompt.send_keys('这是一个prompt弹窗')
        prompt.dismiss()
    #获取时间戳
    def get_time(self):
        return time.strftime("%Y%m%d%H%M%S",time.localtime())
    def screenshot(self):
        self.driver.save_screenshot(self.get_time()+'.png')
    #封装frame框架
    #self.driver.switch_to.frame(self)
    def go_frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    #封装退出frame框架
    def frame_connect(self):
        self.driver.switch_to.default_content()
    #封装select-lindex
    def select_index(self,loc,x):
        index = Select(self.find_element(loc))
        index.select_by_value(x)
    #封装select-value
    def select_value(self,loc,x):
        value=Select(self.find_element(loc))
        value.select_by_value(x)
    #封装select-visible_text
    def select_text(self,loc,info):
        text=Select(self.find_element(loc))
        text.select_by_visible_text(info)
    #封装鼠标双击
    def double(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #封装鼠标三击
    def three_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click(self.find_element(loc)).perform()
    #封装鼠标右击
    def context_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()
    #封装鼠标悬停
    def move_to_elment(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    #封装鼠标拖拽
    def drag_and_drop(self,loc,x,y):
        ActionChains(self.driver).drag_and_drop_by_offset(self.find_element(loc),x,y).perform()
    #封装鼠标按住不放
    def click_and_hold(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()

    #键盘封装(回车)
    def keys_enter(self,loc):
        self.send_keys(loc,Keys.ENTER)

    def key_tab(self,loc):
        self.send_keys(loc,Keys.TAB)
        self.send_keys(loc,Keys.LEFT)
        self.send_keys(loc,Keys.ADD)
        self.send_keys(loc,Keys.DOWN)
        self.send_keys(loc, Keys.META)
        self.send_keys(loc, Keys.ARROW_UP)
        self.send_keys(loc, Keys.ARROW_DOWN)
        self.send_keys(loc,Keys.ARROW_LEFT)
        self.send_keys(loc,Keys.ARROW_RIGHT)
        self.send_keys(loc, Keys.DELETE)
        self.send_keys(loc, Keys.COMMAND)

    def keys_ctrl_a(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')